
//
//  WBStatusRetWeetView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/21.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit
import SnapKit

class WBStatusRetWeetView: UIView {

    // MARK: -  记录自身底部约束
    var retweetViewConstraint: Constraint?
    
    // MARK: -  模型属性
    var status: WBStatusModel? {
        
        didSet {
            
            //设置内容
            contentLabel.text = status?.retweeted_status?.text
            
            //卸载滋生底部约束
            self.retweetViewConstraint?.uninstall()
            
            //判断如果转发微博有无图片
            if status?.retweeted_status?.pic_urls?.count != 0 {
                
                
                pictureView.pictures = status?.retweeted_status?.pic_urls
                
                //设置新的底部约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                //考虑到cell复用的问题  当有图片的时候 pictureView 显示
                pictureView.hidden = false
            }else {
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                //没有图片的时候   pictureView 隐藏
                pictureView.hidden = true
            }
        }
    }
    
    // MARK: -  重写init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    // MARK: -  重写init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置UI
    private func setUpUI() {
        
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(10)
            make.width.equalTo(kScreenW-20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
        }
    }
    
    // MARK: -  懒加载控件
    // MARK: -  懒加载内容Label
    private lazy var contentLabel: UILabel = {
        
        let contentLabel = UILabel()
        contentLabel.font = UIFont.systemFontOfSize(14)
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    
    // MARK: -  懒加载图片view
    private lazy var pictureView: WBPictureCollectionView = WBPictureCollectionView()

}
